﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tool.Core;

namespace Tool
{
    public partial class DeleteListInfo : Form
    {

        #region properties

        public List<ModelList> CurrentModelList { set; get; }


        #endregion




        public DeleteListInfo()
        {
            InitializeComponent(); InitControl();
            CurrentModelList = new List<ModelList>();
        }


        #region event


        private void btnAdd_Click(object sender, EventArgs e)
        {
            for (int i = lstContainer.Items.Count - 1; i >= 0; i--)
            {
                bool isSelected = lstContainer.GetSelected(i);
                if (isSelected)
                {
                    string currentItem = lstContainer.Items[i].ToString();
                    lstContainer.Items.RemoveAt(i);
                    lstCurrent.Items.Add(currentItem);
                }
            }

            ListOperation listOp = new ListOperation();

            CurrentModelList = GetOperationList(listOp);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            for (int i = lstCurrent.Items.Count - 1; i >= 0; i--)
            {
                bool isSelected = lstCurrent.GetSelected(i);
                if (isSelected)
                {
                    string currentItem = lstCurrent.Items[i].ToString();
                    lstCurrent.Items.RemoveAt(i);
                    lstContainer.Items.Add(currentItem);
                }
            }

            ListOperation listOp = new ListOperation();

            CurrentModelList = GetOperationList(listOp);
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            ListOperation.DeleteListInformation(CurrentModelList);
            MessageBox.Show("操作成功！");
        }


        private List<ModelList> GetOperationList(ListOperation listOp)
        {
            List<ModelList> lists = listOp.GetAllListInfo();
            List<ModelList> operationList = new List<ModelList>();
            for (int i = lstCurrent.Items.Count - 1; i >= 0; i--)
            {
                string itemName = lstCurrent.Items[i].ToString();
                ModelList list = listOp.GetByName(itemName, lists);
                if (list != null)
                    operationList.Add(list);
            }
            return operationList;
        }


        private void btnListName_Click(object sender, EventArgs e)
        {
            string str = Environment.CurrentDirectory.ToString();
            string templateFullName = str + @"\templates\ListNameConstantsTemplate.txt";
            string templateStr = ReadTemplateFile(templateFullName);
            string infos = ListOperation.GetListNameConsts(CurrentModelList);

            templateStr = templateStr.Replace("[Code]", infos);
            templateStr = templateStr.Replace("[Namespace]", Common.NameSpace);


            CreateFileAndThenWriteToFile(Common.OutPutURL + Common.EntityName.ListNameConst, templateStr);

        }

        private void GenerateAll_Click(object sender, EventArgs e)
        {
            string str = Environment.CurrentDirectory.ToString();

          //  string templateFullName = str + @"\templates\FieldsConstantsTemplate.txt";
            string templateFullName = str +@"\templates\"+ Common.GetValueFromXml("FieldConstTemplateName");
            string templateStr = ReadTemplateFile(templateFullName);
            //generate model
            foreach (var item in CurrentModelList)
            {
                string infos = ListOperation.GetModels(item);
                string current = templateStr.Replace("[Code]", infos).
                    Replace("[Namespace]", Common.NameSpace).Replace("[ClassName]", "Model" + item.Name);
                CreateFileAndThenWriteToFile(Common.OutPutURL+"Model" + item.Name + ".cs", current);
            }

            //generate fields conts
            foreach (var item in CurrentModelList)
            {
                string infos = ListOperation.GetFieldConsts(item);
                string current = templateStr.Replace("[Code]", infos).
                    Replace("[Namespace]", Common.NameSpace).Replace("[ClassName]", "FieldsConst" + item.Name);
                CreateFileAndThenWriteToFile(Common.OutPutURL + "FieldsConst" + item.Name + ".cs", current);
            }

            //generate business 
            templateFullName = str + @"\templates\" + Common.GetValueFromXml("BusinessTemplateName");


            templateStr = ReadTemplateFile(templateFullName);
            foreach (var item in CurrentModelList)
            {
               string update = ListOperation.GetBusinessUpdate(item);
               string add = ListOperation.GetBusinessAddInfo(item);
               string changeToModel = ListOperation.GetChangeItemToModel(item);
               string current = templateStr.Replace("[ModelToListItem]", add).
                   Replace(" [UpdateModelToListItem]", update).
                   Replace("[ChangeItemToModel]", changeToModel).
                   Replace("[Namespace]", Common.NameSpace).
                   Replace("[ClassName]",  item.Name);
                CreateFileAndThenWriteToFile(Common.OutPutURL + "Business" + item.Name + ".cs", current);
            }
        }


        #endregion

        #region private methods

        private void InitControl()
        {
            ListOperation listOp = new ListOperation();
            List<ModelList> lists = listOp.GetAllListInfo();
            foreach (var item in lists)
            {
                lstContainer.Items.Add(item.Name);
            }

           
        }

        private string ReadTemplateFile(string templatePath)
        {
            string s = string.Empty;
            using (System.IO.StreamReader reader = new System.IO.StreamReader("" + templatePath + ""))
            {
                s = reader.ReadToEnd();
            }
            return s;
        }

        private void CreateFileAndThenWriteToFile(string fullPath, string txt)
        {
            

            using (System.IO.File.Create(fullPath))
            {

            }
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fullPath))
            {
                writer.Write(txt);
            }
        }


        #endregion








    }
}
